package com.kintreda.ieco_server.module.order.service.impl;

import com.kintreda.ieco_server.bean.order.BuyRecord;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.service.impl.BaseServiceImpl;
import com.kintreda.ieco_server.module.order.service.IBuyRecordService;
import com.kintreda.ieco_server.module.order.service.IPlacesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.order.service.impl
 * @Author ZhongYao_H
 * @Date 2019/4/28:17:09
 * @Year 2019 Month 04 Day 28
 * @Date_zh 2019 年 四月 星期日
 */
@Service
public class BuyRecordServicImpl extends BaseServiceImpl<BuyRecord,String> implements IBuyRecordService {

    @Autowired
    IBaseRepository<BuyRecord,String> buyRecordIBaseRepository;
    @Autowired
    IPlacesService placesService;

    @Override
    public Double count(String userId) {
        double count = buyRecordIBaseRepository.sum("select sum(num) as sum from buyrecord where userOrOrgId = ?", new String[]{userId});
        return count;
    }

    @Override
    public Long getOrgCount(String orgId) {
        List<Map<String, Object>> userIdList = buyRecordIBaseRepository.findListBySql("select id from common_user where orgId = ? ", new String[]{orgId});
        long sum = 0;
        for (Map<String, Object> map : userIdList) {
            String userId = map.get("id").toString();
            Double userCount = count(userId);
                long add = (long) (userCount+sum);
                sum = add;
        }
        return sum;
    }

    @Override
    public double countByOrgId(String orgId) {
        List<Map<String, Object>> userIdList = buyRecordIBaseRepository.findListBySql("select id from common_user where orgId = ? ", new String[]{orgId});
        double sum = 0;
        for (Map<String, Object> map : userIdList) {
            Double count = count(map.get("id").toString());
            sum = sum+count;
        }
        return sum;
    }

    @Override
    public double getCountByOrgAdminId(String adminId) {
        List<Map<String, Object>> userIdList = buyRecordIBaseRepository.findListBySql("select adminId from organization where id = ? ", new String[]{adminId});
        double sum = 0;
        for (Map<String, Object> map : userIdList) {
            Double count = count(map.get("adminId").toString());
            sum = sum+count;
        }
        return sum;
    }
}
